/*
 * @(#)PaginationHelper.java $version 2012-3-5
 * 
 * Copyright 2011 RogueDevelopment Gruop. All rights Reserved.
 * ROGUEDEVELOPMENT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nado.extensions.pagination;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.nado.extensions.data.DataHandler;
import com.nado.extensions.data.DataHandlerFactory;

/**
 * @author LEESUNG
 * 
 */
public class PaginationHelper {
	public static PaginationInfo createInfo(String type, int page, int totalRows) {
	    if (StringUtils.isEmpty(type)) {
	        throw new IllegalArgumentException("Pagination type is empty!");
	      }

	      String path = "pagination/configs/config[@type='" + type + "']";
	      DataHandler handler = DataHandlerFactory.create("pagination-data.xml").select(path);
	      int pageSize = NumberUtils.toInt(handler.get("page-size"), 10);
	      int indexSize = NumberUtils.toInt(handler.get("index-size"), 10);

	      PaginationInfo info = new PaginationInfo(page, totalRows, pageSize, indexSize);
	      info.setStepType(handler.get("step-type", "index"));
	      info.setTemplateType(handler.get("template-type", "default"));

	      return info;
	}

	public static PaginationTemplate createTemplate(String type) {
		if (StringUtils.isEmpty(type)) {
			throw new IllegalArgumentException("Pagination template type is empty!");
		}

		String path = "pagination/templates/template[@type='" + type + "']";
		DataHandler handler = DataHandlerFactory.create("pagination-data.xml").select(path);
		PaginationTemplate template = new PaginationTemplate();
		template.setFirst(handler.get("first", ""));
		template.setFirstOff(handler.get("first-off", ""));
		template.setPrev(handler.get("prev", ""));
		template.setPrevOff(handler.get("prev-off", ""));
		template.setIndex(handler.get("index", ""));
		template.setIndexFirst(handler.get("index-first", template.getIndex()));
		template.setIndexLast(handler.get("index-last", template.getIndex()));
		template.setIndexCurrent(handler.get("index-current",template.getIndex()));
		template.setIndexFirstCurrent(handler.get("index-first-current",template.getIndexCurrent()));
		template.setIndexLastCurrent(handler.get("index-last-current",template.getIndexCurrent()));
		template.setSeparator(handler.get("separator", ""));
		template.setNext(handler.get("next", ""));
		template.setNextOff(handler.get("next-off", ""));
		template.setLast(handler.get("last", ""));
		template.setLastOff(handler.get("last-off", ""));

		return template;
	}
}
